import type { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    name: "redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard", // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
        // https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
          alwaysShow: false,
        },
      },
    ],
  },
  {
    path: "/test",
    name: "test",
    component: () => import("@/views/demo/test.vue"),
    meta: { title: "测试", hidden: true },
  },
];

// 动态路由
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/playmanager",
    name: "playmanager",
    redirect: "/playmanager/playlist",
    component: Layout,
    meta: { title: "剧本信息管理", icon: "el-icon-Reading" },
    children: [
      {
        path: "playlist",
        name: "playlist",
        component: () => import("@/views/ggmManager/ggmList.vue"),
        meta: { title: "剧本列表", icon: "el-icon-Reading" },
      },
      {
        path: "playadd",
        name: "playadd",
        component: () => import("@/views/ggmManager/ggmAdd.vue"),
        meta: { title: "添加剧本", icon: "el-icon-Reading", hidden: true },
      },
      {
        path: "playedit",
        name: "playedit",
        component: () => import("@/views/ggmManager/ggmEdit.vue"),
        meta: { title: "编辑剧本", icon: "el-icon-Reading", hidden: true },
      },
    ],
  },
  {
    path: "/categorymanager",
    name: "categorymanager",
    redirect: "/categorymanager/categorylist",
    component: Layout,
    meta: { title: "剧本分类管理" },
    children: [
      {
        path: "categorylist",
        name: "categorylist",
        component: () => import("@/views/categoryManager/categoryList.vue"),
        meta: { title: "分类列表" },
      },
    ],
  },
  {
    path: "/dmmanager",
    name: "dmmanager",
    redirect: "/dmmanager/dmlist",
    component: Layout,
    meta: { title: "dm管理", icon: "el-icon-Mic" },
    children: [
      {
        path: "dmlist",
        name: "dmlist",
        component: () => import("@/views/dmManager/dmList.vue"),
        meta: { title: "dm列表", icon: "el-icon-Mic" },
      },
      {
        path: "dmadd",
        name: "dmadd",
        component: () => import("@/views/dmManager/dmAdd.vue"),
        meta: { title: "添加dm", icon: "el-icon-Mic", hidden: true },
      },
      {
        path: "dmedit",
        name: "dmedit",
        component: () => import("@/views/dmManager/dmEdit.vue"),
        meta: { title: "编辑dm", icon: "el-icon-Mic", hidden: true },
      },
    ],
  },
  {
    path: "/ordermanager",
    name: "ordermanager",
    redirect: "/ordermanager/orderlist",
    component: Layout,
    meta: { title: "订单信息管理", icon: "el-icon-ShoppingTrolley" },
    children: [
      {
        path: "orderlist",
        name: "orderlist",
        component: () => import("@/views/orderManager/orderList.vue"),
        meta: { title: "订单列表" },
      },
    ],
  },
  {
    path: "/sharebillmanager",
    name: "sharebillmanager",
    redirect: "/sharebillmanager/sharebilllist",
    component: Layout,
    meta: { title: "剧本拼单管理" },
    children: [
      {
        path: "sharebilllist",
        name: "sharebilllist",
        component: () => import("@/views/sharebillManager/sharebillList.vue"),
        meta: { title: "拼单列表", icon: "el-icon-List" },
      },
    ],
  },
  {
    path: "/usermanager",
    name: "usermanager",
    redirect: "/usermanager/userlist",
    component: Layout,
    meta: { title: "用户管理" },
    children: [
      {
        path: "userlist",
        name: "userlist",
        component: () => import("@/views/userManager/userList.vue"),
        meta: { title: "用户列表", icon: "el-icon-UserFilled" },
      },
    ],
  },
  {
    path: "/imgattachment",
    name: "imgattachment",
    redirect: "/imgattachment/compereimg",
    component: Layout,
    meta: { title: "附件图片管理", icon: "el-icon-Picture" },
    children: [
      {
        path: "compereimg",
        name: "compereimg",
        component: () => import("@/views/imgAttachment/compereImg.vue"),
        meta: { title: "主持人相册", icon: "el-icon-PictureFilled" },
      },
      {
        path: "scriptcover",
        name: "scriptcover",
        component: () => import("@/views/imgAttachment/scriptCover.vue"),
        meta: { title: "剧本封面图", icon: "el-icon-PictureFilled" },
      },
      {
        path: "scriptrolesimg",
        name: "scriptrolesimg",
        component: () => import("@/views/imgAttachment/scriptRolesImg.vue"),
        meta: { title: "剧本角色图", icon: "el-icon-PictureFilled" },
      },
      {
        path: "managerimg",
        name: "managerimg",
        component: () => import("@/views/imgAttachment/managerImg.vue"),
        meta: { title: "管理员图片", icon: "el-icon-PictureFilled" },
      },
    ],
  },
  {
    path: "/accountmanager",
    name: "accountmanager",
    redirect: "/accountManager/managerlist",
    component: Layout,
    meta: { title: "账号管理", icon: "el-icon-User" },
    children: [
      {
        path: "managerlist",
        name: "managerlist",
        component: () => import("@/views/accountManager/managerList.vue"),
        meta: { title: "管理员列表", icon: "el-icon-UserFilled" },
      },
      {
        path: "manageradd",
        name: "manageradd",
        component: () => import("@/views/accountManager/managerAdd.vue"),
        meta: { title: "添加管理员", hidden: true },
      },
      {
        path: "manageredit",
        name: "manageredit",
        component: () => import("@/views/accountManager/managerEdit.vue"),
        meta: { title: "编辑管理员", hidden: true },
      },
      {
        path: "rolelist",
        name: "rolelist",
        component: () => import("@/views/accountManager/rolesList.vue"),
        meta: { title: "角色列表", icon: "el-icon-UserFilled" },
      },
      {
        path: "permissionlist",
        name: "permissionlist",
        component: () => import("@/views/accountManager/permissionList.vue"),
        meta: { title: "权限列表", icon: "el-icon-UserFilled" },
      },
    ],
  },

  {
    path: "/personalcenter",
    name: "personalcenter",
    redirect: "/personalcenter/updatepersonalinfo",
    component: Layout,
    meta: { title: "个人中心" },
    children: [
      {
        path: "changepassword",
        name: "changepassword",
        component: () => import("@/views/personalCenter/changepassword.vue"),
        meta: { title: "修改密码", icon: "el-icon-Edit", hidden: true },
      },
      {
        path: "updatepersonalinfo",
        name: "updatepersonalinfo",
        component: () =>
          import("@/views/personalCenter/updatePersonalInfo.vue"),
        meta: { title: "修改个人信息", icon: "el-icon-Edit" },
      },
    ],
  },
];

// 错误路由
export const errorRoutes: RouteRecordRaw[] = [
  {
    path: "/401",
    name: "401",
    component: () => import("@/views/error-page/401.vue"),
    meta: { hidden: true },
  },

  {
    path: "/404",
    name: "404",
    component: () => import("@/views/error-page/404.vue"),
    meta: { hidden: true },
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
